package com.xiaoyu.lanling.feature.chat.viewholder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import com.facebook.drawee.view.SimpleDraweeView
import com.qmuiteam.qmui.widget.QMUILoadingView
import com.xiaoyu.im.datamodel.MessageDisplayStatus
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.chat.handler.ChatMessageImageItemClickHandler
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageSendImageItem
import com.xiaoyu.lanling.media.image.ImageViewUtil

class ChatMessageSendImageViewHolder : ChatMessageBaseReceiveSendViewHolder<ChatMessageSendImageItem>() {

    private var mView: View? = null
    private var mFail: ImageButton? = null
    private var mLoading: QMUILoadingView? = null
    private var mSimpleDraweeView: SimpleDraweeView? = null

    override fun doCreateChildrenView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_chat_send_image, parent, false)
        mFail = view.findViewById(R.id.chat_send_fail)
        mLoading = view.findViewById(R.id.chat_send_loading)
        mSimpleDraweeView = view.findViewById(R.id.chat_message_image)
        return view
    }

    override fun showData(position: Int, itemData: ChatMessageSendImageItem) {
        super.showData(position, itemData)

        if (itemData.messageDisplayStatus == MessageDisplayStatus.SENDING) {
            mLoading?.visibility = View.VISIBLE
            mFail?.visibility = View.GONE
        } else if (itemData.messageDisplayStatus == MessageDisplayStatus.NORMAL_FAIL) {
            mFail?.setImageResource(R.drawable.icon_chat_send_fail)
            mFail?.visibility = View.VISIBLE
            mLoading?.visibility = View.GONE
        } else if (itemData.messageDisplayStatus == MessageDisplayStatus.BLACKED_FAIL) {
            mFail?.setImageResource(R.drawable.icon_chat_check_fail)
            mFail?.visibility = View.VISIBLE
            mLoading?.visibility = View.GONE
        } else {
            mFail?.visibility = View.GONE
            mLoading?.visibility = View.GONE
        }
        mSimpleDraweeView?.let {
            it.layoutParams?.width = itemData.width
            it.layoutParams?.height = itemData.height
            it.requestLayout()
            ImageViewUtil.loadImage(it, itemData.imageLoadParam)
        }
        mSimpleDraweeView?.let {
            ChatMessageImageItemClickHandler.bindSendImageItemClickForViewAndData(it, itemData)
            ChatMessageImageItemClickHandler.bindSendImageItemLongClickForViewAndData(it, itemData)
        }
//        ChatMessageSendFailClickHandler.bindImageSendFailClickForViewAndData(mFail, itemData)
    }
}